<template>
  <div class="home">
    <div class="swiper-container">
    <div class="swiper-wrapper">
        <div class="swiper-slide"
        v-for="banner in banners"
        :key="banner.id">
          <img :src="banner.imageUrl">
        </div>
    </div>
    <!-- 如果需要分页器 -->
    <div class="swiper-pagination"></div>
    <!-- 如果需要导航按钮 -->
    <div class="swiper-button-prev"></div>
    <div class="swiper-button-next"></div>
</div>
  </div>
</template>

<script>
import { getBanners } from '@/api/home'
import Swiper from 'swiper'
import 'swiper/swiper.scss'
export default {
  name: 'Home',
  data () {
    return {
      banners: []
    }
  },
  created () {
    getBanners().then(res => {
      console.log(res)
      this.banners = res
      // 在下次DOM更新后，创建Swiper 对象
      this.$nextTick(() => {
        /* eslint-disable-next-line */
        new Swiper ('.swiper-container', {
          loop: true, // 循环模式选项
          // 如果需要分页器
          pagination: {
            el: '.swiper-pagination',
            clickable: true
          },
          autoplay: true,
          // 如果需要前进后退按钮
          navigation: {
            nextEl: '.swiper-button-next',
            prevEl: '.swiper-button-prev'
          }
        })
      })
    })
  }
}

</script>

<style lang="scss" scoped>
  .home {
    .swiper-container {
      width: 100%;
      height: 100%;
      img{
        width:100%;
      }
    }
  }
</style>
